package com.example.li.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;

import java.util.List;

public class aActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private EMMessageListener msgListener;
    private TextView id;
    private EditText msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        into();
        intoView();
    }

    private void intoView() {
        id = findViewById(R.id.id);
        msg = findViewById(R.id.msg);
    }

    private void into() {
        msgListener = new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                //收到消息
                Log.d(TAG, "收到消息: "+messages);
            }

            @Override
            public void onCmdMessageReceived(List<EMMessage> messages) {
                //收到透传消息
                Log.d(TAG, "收到透传消息: "+messages);
            }

            @Override
            public void onMessageRead(List<EMMessage> messages) {
                //收到已读回执
                Log.d(TAG, "收到已读回执: "+messages);
            }

            @Override
            public void onMessageDelivered(List<EMMessage> messages) {
                //收到已送达回执
                Log.d(TAG, "收到已送达回执: "+messages);
            }

            @Override
            public void onMessageChanged(EMMessage message, Object change) {
                //消息状态变动
                Log.d(TAG, "消息状态变动: "+message);
            }

        };
        EMClient.getInstance().chatManager().addMessageListener(msgListener);
    }

    public void fs(View view) {
        String id = this.id.getText().toString();
        String msg = this.msg.getText().toString();
        EMMessage message = EMMessage.createTxtSendMessage(msg, id);
//如果是群聊，设置chattype，默认是单聊
//        if (chatType == CHATTYPE_GROUP)
//            message.setChatType(EMMessage.ChatType.GroupChat);
//发送消息
        EMClient.getInstance().chatManager().sendMessage(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //记得在不需要的时候移除listener，如在activity的onDestroy()时
        EMClient.getInstance().chatManager().removeMessageListener(msgListener);
    }
}
